package com.slipper.system.codeless.service;

import java.util.Collections;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.slipper.util.Tool;
import com.slipper.util.UUID;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import com.slipper.system.codeless.dao.DataFormModelDao;
import com.slipper.system.codeless.bo.DataFormModelBO;

import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;

@Service
@ReadTransactional
public class DataFormModelService {
    @Resource
    private DataFormModelDao dataFormModelDao;
    @Resource
    private DataFormFieldService dataFormFieldService;
    
    public DataFormModelBO queryBy(String id) {
        return dataFormModelDao.queryBy(id);
    }
    
    public List<DataFormModelBO> queryList(DataFormModelBO bo) {
        return dataFormModelDao.queryList(bo);
    }
    
    public PageInfo<DataFormModelBO> queryPage(DataFormModelBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(dataFormModelDao.queryList(bo));
    }
    
    @WriteTransactional
    public int save(String formId, List<DataFormModelBO> list, String employeeId) {
        if (Tool.isBlank(formId)) {
            return 0;
        }

        list.forEach(model -> {
            model.setFormId(formId);
            if (Tool.isBlank(model.getId())) {
                model.setId(UUID.getUUID());
                model.setUpdateId(employeeId);
                model.setCreateId(employeeId);
                model.setUpdateTime(System.currentTimeMillis());
                model.setCreateTime(System.currentTimeMillis());
                dataFormModelDao.insert(model);
            } else {
                model.setUpdateId(employeeId);
                model.setUpdateTime(System.currentTimeMillis());
                dataFormModelDao.update(model);
            }

            dataFormFieldService.save(model.getId(), model.getFieldList(), employeeId);
        });

        // 删除被删除的数据
        DataFormModelBO qo = new DataFormModelBO();
        qo.setFormId(formId);
        dataFormModelDao.queryList(qo).stream()
                .filter(model -> list.stream().noneMatch(temp -> model.getId().equals(temp.getId())))
                .forEach(this::delete);

        return 1;
    }

    @WriteTransactional
    public void saveList(List<DataFormModelBO> list) {
        if (list == null) return;

        list.forEach(item -> {
            if (dataFormModelDao.queryBy(item.getId()) != null) {
                item.setUpdateTime(System.currentTimeMillis());
                dataFormModelDao.update(item);
            } else {
                item.setUpdateTime(System.currentTimeMillis());
                item.setCreateTime(System.currentTimeMillis());
                dataFormModelDao.insert(item);
            }
        });
    }

    @WriteTransactional
    public void deleteByFormId(String formId) {
        if (Tool.isBlank(formId)) {
            return;
        }

        DataFormModelBO qo = new DataFormModelBO();
        qo.setFormId(formId);
        dataFormModelDao.queryList(qo).forEach(this::delete);
    }

    @WriteTransactional
    public void delete(DataFormModelBO bo) {
        if (Tool.isBlank(bo.getId())) {
            return;
        }

        dataFormFieldService.deleteByModelId(bo.getId());
        dataFormModelDao.delete(bo.getId());
    }

    @WriteTransactional
    public void copyByFormId(String oldFormId, String newFormId, String employeeId) {
        if (Tool.isBlank(oldFormId) || Tool.isBlank(newFormId)) {
            return;
        }

        DataFormModelBO qo = new DataFormModelBO();
        qo.setFormId(oldFormId);
        dataFormModelDao.queryList(qo).forEach(formModel -> {
            String oldModelId = formModel.getId();

            formModel.setId(UUID.getUUID());
            formModel.setFormId(newFormId);
            formModel.setUpdateId(employeeId);
            formModel.setCreateId(employeeId);
            formModel.setUpdateTime(System.currentTimeMillis());
            formModel.setCreateTime(System.currentTimeMillis());
            dataFormModelDao.insert(formModel);

            dataFormFieldService.copyByModelId(oldModelId, formModel.getId(), employeeId);
        });
    }

    public List<DataFormModelBO> queryByFormId(String formId) {
        if (Tool.isBlank(formId)) {
            return Collections.emptyList()  ;
        }

        DataFormModelBO qo = new DataFormModelBO();
        qo.setFormId(formId);
        List<DataFormModelBO> list = dataFormModelDao.queryList(qo);
        list.forEach(model -> model.setFieldList(dataFormFieldService.queryByModelId(model.getId())));
        return list;
    }
}
